﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DeliveryService
{
    public class BLFavorite
    {
        /// <summary>
        /// 添加商品到收藏夹
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="goodsId">商品ID</param>
        /// <returns>0:添加成功  1:添加失败  2:已经收藏过了</returns>
        public int AddGoodsToFavorite(int studentId, int goodsId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                try
                {
                    if (IsThisMyFavoriteGoods(studentId, goodsId))
                        return 2;
                    MyGoodsFavorite gf = new MyGoodsFavorite();
                    gf.Good = db.Goods.Single(g => g.Goods_Id == goodsId);
                    gf.Student = db.Students.Single(s => s.Students_Id == studentId);
                    db.MyGoodsFavorites.InsertOnSubmit(gf);
                    db.SubmitChanges();
                    return 0;
                }
                catch
                {

                    return 1;
                }

            }
        }

        public bool IsThisMyFavoriteGoods(int studentId, int goodsId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.MyGoodsFavorites.FirstOrDefault(g => g.Student.Students_Id == studentId && g.Good.Goods_Id == goodsId);
                return query != null;
            }
        }

        public bool IsThisMyFavoriteFoods(int studentId, int foodsId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.MyFoodsFavorites.FirstOrDefault(g => g.Student.Students_Id == studentId && g.Food.Foods_Id == foodsId);
                return query != null;
            }
        }

        /// <summary>
        /// 添加菜品到收藏夹
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="FoodsId">菜品ID</param>
        /// <returns>0:添加成功  1:添加失败  2:已经收藏过了</returns>
        public int AddFoodsToFavorite(int studentId, int foodsId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                try
                {
                    if (IsThisMyFavoriteFoods(studentId, foodsId))
                        return 2;
                    MyFoodsFavorite ff = new MyFoodsFavorite();
                    ff.Food = db.Foods.Single(f => f.Foods_Id == foodsId);
                    ff.Student = db.Students.Single(s => s.Students_Id == studentId);
                    db.MyFoodsFavorites.InsertOnSubmit(ff);
                    db.SubmitChanges();
                    return 0;
                }
                catch
                {
                    return 1;
                }

            }
        }
        /// <summary>
        /// 获取学生收藏的商品列表
        /// </summary>
        /// <param name="studentId"></param>
        /// <returns></returns>
        public List<MFavoriteFoods> GetStudentsFavoriteFoods(int studentId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.MyFoodsFavorites.Where(f => f.Student.Students_Id == studentId)
                              .Select(f => new MFavoriteFoods
                                        {
                                            FavoriteFoodId = f.Id,
                                            FoodId = f.Food.Foods_Id,
                                            FoodName = f.Food.Foods_Name,
                                            FoodPicture = f.Food.Foods_Pic,
                                            Price = f.Food.Foods_Price.HasValue ? (double)f.Food.Foods_Price.Value : 0,
                                            RestaurantName = f.Food.Restaurant.Restaurant_Name,
                                            IsBusy = f.Food.Restaurant.IsBusy.HasValue ? (bool)f.Food.Restaurant.IsBusy.Value : false,
                                            RestaruantAddress = f.Food.Restaurant.Restaurant_Address
                                        });
                return query.ToList();
            }
        }
        /// <summary>
        /// 获取学生收藏的菜品列表
        /// </summary>
        /// <param name="studentId"></param>
        /// <returns></returns>
        public List<MFavoriteGoods> GetStudentsFavoriteGoods(int studentId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                // Good.IsSpecialGoods==1表示有货
                var query = db.MyGoodsFavorites.Where(g => g.Student.Students_Id == studentId && g.Good.IsSpecialGoods == 1)
                            .Select(g => new MFavoriteGoods
                                            {
                                                FavoriteGoodsId = g.Id,
                                                GoodsId = g.Good.Goods_Id,
                                                GoodsName = g.Good.Goods_Name,
                                                GoodsPictureUrl = g.Good.Goods_Pic,
                                                GoodsInfo = g.Good.Goods_info,
                                                Price = g.Good.Goods_Price.HasValue ? (double)g.Good.Goods_Price.Value : 0,
                                                IsSpecialGoods = g.Good.IsSpecialGoods == 1,
                                                BrandName = g.Good.Brand.Brand_Name,
                                                GoodsStandard = g.Good.Goods_Standard,
                                                GoodsSamllCategoryName = g.Good.Goodscategory_.GoodsCategory_S_Name
                                            });
                return query.ToList();
            }
        }
        /// <summary>
        /// 删除收藏的商品
        /// </summary>
        /// <param name="studentId"></param>
        /// <param name="goodsIdList">商品ID,以逗号分隔</param>
        /// <returns></returns>
        public bool DeleteFavoriteGoods(int studentId, string goodsIdList)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.MyGoodsFavorites.Where(g => g.Student.Students_Id == studentId && goodsIdList.Split(',').Contains(g.Good.Goods_Id.ToString()));
                db.MyGoodsFavorites.DeleteAllOnSubmit(query);
                db.SubmitChanges();
                return true;
            }
        }

        /// <summary>
        /// 删除收藏的菜品
        /// </summary>
        /// <param name="studentId"></param>
        /// <param name="foodsIdList">菜品ID,以逗号分隔</param>
        /// <returns></returns>
        public bool DeleteFavoriteFoods(int studentId, string foodsIdList)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.MyFoodsFavorites.Where(f => f.Student.Students_Id == studentId && foodsIdList.Split(',').Contains(f.Food.Foods_Id.ToString()));
                db.MyFoodsFavorites.DeleteAllOnSubmit(query);
                db.SubmitChanges();
                return true;
            }
        }
    }
}